package com.joelaws.prototype.server.blackberry;

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Router;
import org.restlet.data.Protocol;

public class RestletApp extends Application {

	private static Component component;

	public RestletApp(Context parentContext) {
		super(parentContext);
	}

	@Override
	public synchronized Restlet createRoot() {
		Router router = new Router(getContext());

		router.attachDefault(Unknown.class);

		router.attach(UserMeetingList.getURL(), UserMeetingList.class);

		router.attach(UserLogin.getURL(), UserLogin.class);

		router.attach(UserList.getURL(), UserList.class);

		router.attach(MeetUser.getURL(), MeetUser.class);

		router.attach(UserPicture.getURL(), UserPicture.class);

		router.attach(UserInfo.getURL(), UserInfo.class);

		router.attach(Reset.getURL(), Reset.class);

		return router;
	}

	public synchronized static void activate() {

		if (component == null) {
			System.out.println("Activating restlet framework");
			component = new Component();

			component.getServers().add(Protocol.HTTP, 8089);

			component.getDefaultHost().attach(
					new RestletApp(component.getContext()));

			try {
				component.start();
			} catch (Exception e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void main(String[] argv) {
		RestletApp.activate();
	}
}
